﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace HoaDon
{
	public partial class DM_DoiTac : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				lbError.Text = "";
				BindData();
			}
		}

		private void BindData()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmdoitac WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Kh_yn = 1 AND Status = '1'");

			gdvDoiTac.DataSource = dt;
			gdvDoiTac.DataBind();
			if (dt.Rows.Count == 0)
			{
				DataRow dtr = dt.NewRow();
				dt.Rows.Add(dtr);
				gdvDoiTac.DataSource = dt;
				gdvDoiTac.DataBind();
				gdvDoiTac.Rows[0].Enabled = false;
			}
		}

		protected void EditDM(object sender, GridViewEditEventArgs e)
		{
			gdvDoiTac.EditIndex = e.NewEditIndex;
			BindData();
			lbError.Text = "";
		}
		protected void CancelEditDM(object sender, GridViewCancelEditEventArgs e)
		{
			gdvDoiTac.EditIndex = -1;
			BindData();
			lbError.Text = "";
		}

		protected void OnPaging(object sender, GridViewPageEventArgs e)
		{
			BindData();
			gdvDoiTac.PageIndex = e.NewPageIndex;
			gdvDoiTac.DataBind();
			lbError.Text = "";
		}

		protected void UpdateDM(object sender, GridViewUpdateEventArgs e)
		{
			string id = ((Label)gdvDoiTac.Rows[e.RowIndex].FindControl("DoiTacID")).Text;
			string name = ((TextBox)gdvDoiTac.Rows[e.RowIndex].FindControl("tbDoiTacName")).Text;
			string add = ((TextBox)gdvDoiTac.Rows[e.RowIndex].FindControl("tbDoiTacAdd")).Text;
			string tel = ((TextBox)gdvDoiTac.Rows[e.RowIndex].FindControl("tbDoiTacTel")).Text;
			if (id == "")
			{
				lbError.Text = "Không để trống mã";
				return;
			}
			if (name == "")
			{
				lbError.Text = "Không để trống tên";
				return;
			}
			SqlEXEC.ExecCommand(Session["strConn"].ToString(), "UPDATE dmdoitac SET DoiTacName = N'" + name + "', DoiTacAdd = N'" + add + "', DoiTacTel = '" + tel + "',user_id2='" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "',datetime2=GETDATE() WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND DoiTacID= '" + id + "'");
			gdvDoiTac.EditIndex = -1;
			BindData();
			lbError.Text = "";
		}

		protected void DeleteDM(object sender, EventArgs e)
		{
			Button bt = (Button)sender;
			if (bt.CommandArgument.ToUpper() == "AKL")
			{
				lbError.Text = "Mã khách lẻ, không được xóa";
				return;
			}
			try
			{
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), "DELETE FROM dmdoitac WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND DoiTacID= '" + bt.CommandArgument + "'");
				lbError.Text = "";
			}
			catch (Exception ex)
			{
				lbError.Text = "Đã phát sinh dữ liệu không thể xóa";
			}
			BindData();
		}
		protected void AddNewDM(object sender, EventArgs e)
		{
			string id = ((TextBox)gdvDoiTac.FooterRow.FindControl("txtDoiTacID")).Text;
			string name = ((TextBox)gdvDoiTac.FooterRow.FindControl("txtDoiTacName")).Text;
			string add = ((TextBox)gdvDoiTac.FooterRow.FindControl("txtDoiTacAdd")).Text;
			string tel = ((TextBox)gdvDoiTac.FooterRow.FindControl("txtDoiTacTel")).Text;
			if (id == "")
			{
				lbError.Text = "Không để trống mã";
				return;
			}
			if (name == "")
			{
				lbError.Text = "Không để trống tên";
				return;
			}
			string sCommand = "INSERT INTO dmdoitac (DoiTacID,FK_DatabaseID,Code,DoiTacName,DoiTacName2,DoiTacAdd,DoiTacTel,DoiTacFax,DoiTacEmail,FK_Nhdt2ID,Nv_yn,Kh_yn,Ncc_yn,FK_NxID_nv,FK_NxID_kh,FK_NxID_ncc,Status,user_id0,user_id2,datetime0,datetime2) VALUES ('";
			sCommand = sCommand + id + "','";
			sCommand = sCommand + Session["FK_DatabaseID"].ToString() + "','";
			sCommand = sCommand + "" + "',N'";
			sCommand = sCommand + name + "',N'";
			sCommand = sCommand + "" + "',N'";
			sCommand = sCommand + add + "','";
			sCommand = sCommand + tel + "','";
			sCommand = sCommand + "" + "','";
			sCommand = sCommand + "" + "','";
			sCommand = sCommand + "" + "',";
			sCommand = sCommand + "0" + ",";
			sCommand = sCommand + "1" + ",";
			sCommand = sCommand + "0" + ",'";
			sCommand = sCommand + "BTM" + "','";
			sCommand = sCommand + "BTM" + "','";
			sCommand = sCommand + "BTM" + "','";
			sCommand = sCommand + "1" + "','";

			sCommand = sCommand + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "','";
			sCommand = sCommand + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "',";
			sCommand = sCommand + "GETDATE()" + ",";
			sCommand = sCommand + "GETDATE()" + ")";

			if (SqlEXEC.ExecCommand(Session["strConn"].ToString(), sCommand) == -1)
				lbError.Text = "Mã đã tồn tại, chọn mã khác";
			else
			{
				BindData();
				lbError.Text = "";
			}
		}
	}
}
